package com.jy.show.controller;

import com.jy.show.bean.User;
import com.jy.show.common.ResultCode;
import com.jy.show.common.ResultCommon;
import com.jy.show.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@Api("用户模块接口")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    @ResponseBody
    @ApiOperation(value = "用户登录")
    public ResultCommon UserLogin(User user, HttpSession session){
        int count = userService.UserLogin(user);
        ResultCommon result = null;
        if (count >= 1){
            result = ResultCommon.success(ResultCode.LOGIN_SUCCESS);
            session.setAttribute("user",user);
            return result;
        }else {
            result = ResultCommon.fail(ResultCode.LOGIN_FAIL);
            return  result;
        }
    }

    @PostMapping("/regist")
    @ResponseBody
    @ApiOperation(value = "用户注册")
    public ResultCommon regist(User user,HttpSession session){
        ResultCommon result = null;
        int count = userService.regist(user);
        if (count == 1){
            result = ResultCommon.success(ResultCode.REGIST);
            session.setAttribute("user",user);
            return  result;
        }else {
            result = ResultCommon.fail(ResultCode.REGIST_FAIL);
            return result;
        }
    }


    @RequestMapping("/findAll")
    @ResponseBody
    @ApiOperation(value = "所有数据")
    public ResultCommon findAll(){
        ResultCommon result = null;
        List<User> allUser = userService.findAll();
        result = ResultCommon.success(ResultCode.ALL_SUCCESS,allUser);
        return result;
    }
}

